package ttk.muxiuesd.event;

import ttk.muxiuesd.entity.Entity;
import ttk.muxiuesd.entity.EntityGroup;

import java.util.ArrayList;
import java.util.List;

/**
 * 有实体死亡就调用这个事件
 * */
public class EntityDead{

    private List<EntityDeadEvent> events = new ArrayList<>();
    private List<EntityDeadEvent> delayAdd = new ArrayList<>();
    private List<EntityDeadEvent> delayRemove = new ArrayList<>();

    /**
     * 事件处理
     * @param entity 死亡的实体
     * */
    public void handle(EntityGroup entityGroup, Entity entity) {
        //先执行事件
        for (EntityDeadEvent event : events) {
            event.execute(entityGroup, entity);
        }

        //再增减事件
        if (!this.delayAdd.isEmpty()) {
            this.events.addAll(this.delayAdd);
            delayAdd.clear();
        }
        if (!this.delayRemove.isEmpty()) {
            this.events.removeAll(this.delayRemove);
            this.delayRemove.clear();
        }
    }

    public boolean addEvent (EntityDeadEvent event) {
        boolean flag = false;
        if (!this.events.contains(event)) {
            this.delayAdd.add(event);
            flag = true;    //添加成功为true
        }
        return flag;
    }
    public boolean removeEvent (EntityDeadEvent event) {
        boolean flag = false;
        if (this.events.contains(event)) {
            this.delayRemove.remove(event);
            flag = true;
        }
        return flag;
    }
}
